<?php
/**
 * Created by PhpStorm.
 * User: HanSon
 * Date: 2016/10/17
 * Time: 13:16
 */

namespace App\Service;

//use zgldh\QiniuStorage\QiniuStorage;
//use itbdw\QiniuStorage\QiniuStorage;
use App\Exceptions\WechatException;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;

class QiniuService
{

    protected $disk;

    protected $domain;

    protected $token;

    public function __construct()
    {
        $auth = new Auth(env('QINIU_ACCESS'), env('QINIU_SECRET'));

        $this->token = $auth->uploadToken(env('QINIU_BUCKET'));

        $this->domain = env('QINIU_DOMAIN');
    }

    public function put($file, $url)
    {
        $upManager = new UploadManager();

        list($ret, $error) = $upManager->put($this->token, $file, $url);

        if($error){
            \Log::error($error->message());
            throw new WechatException('上传图片出现问题，请重新上传');
        }

        return 'http://' . $this->domain . '/' . $ret['key'];
    }
}